package com.commerce.recording.utils;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShareManager {

    //批量分享文件
    public static void shareFiles(Context context, List<File> fileList) {
        //检查是否为空
        if(context == null || fileList == null || fileList.size() < 1) {
            return;
        }
        ArrayList<Uri> uriList = new ArrayList();
        //遍历成uri
        for(File file : fileList) {
            Uri uri = Uri.fromFile(file);
            uriList.add(uri);
        }
        Intent intent = null;
        //判断是否为单个分享
        boolean isMultiple = uriList.size() > 1;
        intent = new Intent(isMultiple ? Intent.ACTION_SEND_MULTIPLE : Intent.ACTION_SEND);
        intent.setType("audio/*");
        if(isMultiple) {
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
        }else {
            intent.putExtra(Intent.EXTRA_STREAM, uriList.get(0));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //启动分享
        Intent chooser = Intent.createChooser(intent, "Choose a channel to share your files...");
        context.startActivity(chooser);
    }

}
